var noteType = {
    EM_NONE: 0, //无效
    EM_TOUCH: 1, //点击
    EM_LEFT: 2, //左
    EM_RIGHT: 3, //右
    EM_UP: 4, //上
    EM_DOWN: 5, //下
    EM_SLUR: 6, //连音
    EM_BOOM: 7, //炸弹
    EM_Fast: 8, //快速
    EM_Double: 9 //双击
    //EM_SELECT : 8, //选择音符
};

const fs = require("fs");


var exportOsufile = function (fileName) {

    var p = fileName.split(".");
    var name = p[1].split("/");
    name = name[name.length - 1];

    var ext = p[p.length - 1];

    if (ext != "osu") {
        return;
    }

    var realFileName = fileName;
    var fileData = fs.readFileSync(realFileName);

    var str = fileData.toString();

    //取得歌曲名字

    var MP3patt = /AudioFilename:.*/g;
    var mp3Result = str.match(MP3patt);
    mp3Result = mp3Result.toString();

    var musicName = mp3Result.replace("AudioFilename: ", "");
    console.log("musicName: " + musicName);

    /**
     * 取得音轨时间
     */
    var patt = /\[HitObjects\](\r|\n)(.*(\r|\n))*/g;
    var result = str.match(patt);
    // console.log(result);

    str = result.toString();
    result = str.replace("[HitObjects]\r\n", "")
    // console.log(result);

    var notes = result.toString().split("\r\n");

    var config = {};

    config.music = musicName;
    config.delay = 1;
    config.list = [];

    config.version = 1;

    for (var i = 0; i < notes.length - 1; ++i) {

        var s = notes[i];
        var nums = s.split(",");

        var note = {};
        note.id = parseInt(i) + 1;
        note.time = parseFloat(nums[2]) / 1000;
        note.type = 1;
        note.moveTime = MoveTime;
        note.birthY = 375;
        note.ext1 = 0;
        config.list.push(note);
    }

    fs.writeFileSync("./musicConfig/" + name + ".json", JSON.stringify(config));


    var temp = fileName.split("/");

    temp = temp[temp.length - 1];

    fs.renameSync(realFileName, "./osuBack/" + temp);

};

var exportOsuDir = function (fileName) {


    if (fileName) {

        exportOsufile(fileName);

    } else {

        if (fs.existsSync(fileDirectory)) {

            var files = fs.readdirSync(fileDirectory);
            files.forEach(function (filename) {

                var realFileName = fileDirectory + "/" + filename;

                exportOsufile(realFileName);

            });

        } else {
            console.log(fileDirectory + "  Not Found!");
        }
    }

}


var fileDirectory = "./osu"

var MoveTime = 1.5;

exportOsuDir();